package com.example.boot.dlorder.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.boot.ResultEnum.ResultEnum;
import com.example.boot.dlgoods.dao.DlGoodsMapper;
import com.example.boot.dlgoods.model.DlGoods;
import com.example.boot.dlgoodspic.dao.DlGoodsPicMapper;
import com.example.boot.dlgoodspic.model.DlGoodsPic;
import com.example.boot.dlorder.dao.DlOrderMapper;
import com.example.boot.dlorder.model.DlOrder;
import com.example.boot.dlorder.service.DlOrderServic;
import com.example.boot.dlorderdetail.dao.DlOrderDetailMapper;
import com.example.boot.dlorderdetail.model.DlOrderDetail;
import com.example.boot.exception.BootException;
import com.example.boot.util.JSONToObj;
import com.example.boot.util.NewUUID;
import com.example.boot.util.OrderNoCreate;

import net.sf.json.JSONArray;

@Service
public class DlOrderImpl implements DlOrderServic {
	@Autowired
	private DlOrderMapper dlOrderMapper;
	@Autowired
	private DlOrderDetailMapper dlOrderDetailMapper;
	@Autowired
	private DlGoodsPicMapper dlGoodsPicMapper;

	@Transactional
	@Override
	public DlOrder saveOrder(String orderData, String detailData) throws Exception {
		DlOrder order = null;
		List<DlOrderDetail> detailList = new ArrayList<>();

		try {

			OrderNoCreate orderNo = new OrderNoCreate();
			JSONToObj jo = new JSONToObj();
			orderNo.createNo("a");
			String orderUuid = NewUUID.UUID();//
			order = (DlOrder) jo.JsonToClass(orderData, new DlOrder());
			order.setId(orderUuid);
			order.setCreateDate(new Date());
			order.setUpdateDate(new Date());
			order.setBpmStatus(0);
			order.setStatus(ResultEnum.ORDER_STATUS_ZERO.getCode());
			dlOrderMapper.insertSelective(order);
			net.sf.json.JSONArray detailObjStr = JSONArray.fromObject(detailData);
			for (int i = 0; i < detailObjStr.size(); i++) {
				String rto = detailObjStr.get(i).toString();
				DlOrderDetail jsonToClass = (DlOrderDetail) jo.JsonToClass(rto, new DlOrderDetail());
				jsonToClass.setId(NewUUID.UUID());
				dlOrderDetailMapper.insertSelective(jsonToClass);
				detailList.add(jsonToClass);
			}
			order.setDlDetailList(detailList);
			order.setGoodsPic(dlGoodsPicMapper.byGoodsIdFindPic(order.getGoodsId()));
		} catch (Exception e) {
			throw new BootException(ResultEnum.SYSTEM_EXC);
		}

		return order;
	}

	@Override
	public List<DlOrder> findUserOrder(String openId, Integer status) throws Exception {
		List<DlOrder> byOpenIdFindOrder = dlOrderMapper.byOpenIdFindOrder(openId, status);
		for (DlOrder order : byOpenIdFindOrder) {
			List<DlOrderDetail> byOrderIdFindDetail = dlOrderDetailMapper.byOrderIdFindDetail(order.getId());
			order.setDlDetailList(byOrderIdFindDetail);
			String byGoodsIdFindPic = dlGoodsPicMapper.byGoodsIdFindPic(order.getGoodsId());
			order.setGoodsPic(byGoodsIdFindPic);
		}

		return byOpenIdFindOrder;
	}

	@Override
	public Boolean updateStatus(String orderId, Integer status) throws Exception {
		DlOrder order= new DlOrder();
		order.setId(orderId);
		order.setStatus(status);
		
		return dlOrderMapper.updateByPrimaryKeySelective(order)==1?true:false;
	}

}
